<template>
  <div class="reset">
    <el-form
      ref="resetForm"
      :model="resetForm"
      :rules="resetRules"
      class="reset-form"
    >
      <h3 class="title">重置密码</h3>
      <el-form-item prop="phoneNum">
        <el-input
          v-model="resetForm.phoneNum"
          type="text"
          auto-complete="off"
          placeholder="手机号"
        >
          <svg-icon
            slot="prefix"
            icon-class="phone"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>

      <el-form-item prop="code" v-if="captchaOnOff">
        <el-input
          v-model="resetForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="validCode"
            class="el-input__icon input-icon"
          />
        </el-input>
        <div class="reset-companyCount">
          <el-button
            style="width: 100%"
            @click="getCompanyCount"
            v-if="!loadingCode"
          >
            <span style="color: deepskyblue">获取验证码</span>
          </el-button>
          <span v-else>{{ countCode }}秒后再获取</span>
        </div>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="resetForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="password"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="repassword">
        <el-input
          v-model="resetForm.repassword"
          type="password"
          auto-complete="off"
          placeholder="确认密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="prefix"
            icon-class="password"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <a href="/login"
        ><h5
          style="
            color: #1890ff;

            margin-bottom: 25px;
          "
        >
          返回登录
        </h5></a
      >
      <el-form-item style="width: 100%">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right" v-if="register">
          <router-link class="link-type" :to="'/register'"
            >立即注册</router-link
          >
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-reset-footer">
      <span
        >Copyright © 2022 Nanjing Co-Innovation Center for 5G and Industrial
        Internet. All Rights Reserved.</span
      >
    </div>
  </div>
</template>

<script>
import { getCode, reset } from "@/api/system/resetpass";

export default {
  name: "Resetpass",
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.resetForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      resetForm: {
        phoneNum: "",
        password: "",
        code: "",
        uuid: "",
      },
      resetRules: {
        phoneNum: [
          { required: true, trigger: "blur", message: "请输入您的手机号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        repassword: [
          {
            required: true,
            validator: validatePass2,
            trigger: "blur",
            message: "密码不一致",
          },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },

      loading: false,
      loadingCode: false,
      countCode: 60,
      timer: null,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {},
  methods: {
    getCompanyCount() {
      if (this.resetForm.phoneNum) {
        getCode(this.resetForm.phoneNum).then((response) => {
          console.log(response);
          this.resetForm.uuid = response.uuid;
          this.loadingCode = true;
          this.time();
        });
      }
    },

    // 开启定时器
    time() {
      // 父组件调用方法时传参来决定是否开启定时器
      this.timer = setInterval(() => {
        this.countCode--;
        console.log(this.countCode);
        // 当计数变量值变为0时关闭定时器
        if (this.countCode == 0) {
          this.close(this.timer);
        }
      }, 1000);
    },
    // 关闭定时器
    close(timer) {
      clearInterval(timer);
      // 把计数变量恢复初始值,以供下次使用
      this.countCode = 60;
      // 修改父组件传过来的控制子组件是否展示的变量值,来关闭子组件
      this.loadingCode = false;
    },

    // time() {
    //   if (this.countCode <= 0) {
    //     this.loadingCode = false;
    //     return;
    //   } else {
    //     let that = this;
    //     setInterval(function () {
    //       that.countCode--;
    //       console.log(that.countCode);
    //     }, 1000);
    //   }
    // },

    handleLogin() {
      this.$refs.resetForm.validate((valid) => {
        if (valid) {
          reset(this.resetForm).then((response) => {
            this.$modal.msgSuccess("修改密码成功");
            this.$router.push({ path: "/login" });
          });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.reset {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.reset-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.reset-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.reset-companyCount {
  width: 33%;
  height: 38px;
  float: right;
  display: flex;
  justify-content: center;
  border: 2px solid deepskyblue;
  border-radius: 5px;
}
.el-reset-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
</style>
